/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Connection.SQLConnection;
import Entities.NHANVIEN;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javax.swing.JOptionPane;

/**
 * FXML Controller class
 *
 * @author lainh_000
 */
public class FX_loginController implements Initializable {

    /**
     * Initializes the controller class.
     */
    
    @FXML  TextField txt_username;
    @FXML  TextField txt_pass;
    @FXML  Button btn_login;
    @FXML  Button btn_exit;
    
    String username , pass="";
    
    SQLConnection connect = new SQLConnection();
    
    NHANVIEN nhanvien_model = new NHANVIEN();
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        assert btn_login != null;
        assert btn_exit !=null;
    }    
    
     public void btn_username_Click(ActionEvent event) throws IOException {
         if(Check()==true)
         {
             //đóng window login lại
        Stage stage_login = (Stage) btn_login.getScene().getWindow();
        stage_login.close();
        //mở window main lên
        Parent root = FXMLLoader.load(getClass().getResource("FX_Main.fxml"));        
        Scene scene_main = new Scene(root);
        Stage stage_main = new Stage();
        stage_main.setScene(scene_main);
        stage_main.setTitle("Quản Lý Cửa Hàng Sách");
        stage_main.show();
         }
         else
         {
             String stb="Username hoặc password bị sai";
            JOptionPane.showMessageDialog(null,stb);
         }
        
    }
     
     public void btn_exit_Click(ActionEvent event) 
     {
         Stage stage = (Stage) btn_exit.getScene().getWindow();
         stage.close();
     }
    
     protected boolean Check()
     {
         username = txt_username.getText();
         pass = txt_pass.getText();
         if("".equals(username) || "".equals(pass))
         {
            String stb="Vui lòng nhập username và password";
            JOptionPane.showMessageDialog(null,stb);
         }
         else
         {

             if(nhanvien_model.getNhanVien_By_Username_Password(username, pass)!= null)
             {
                 return true;
             }
         }
         return false;
     }
}
